#!/bin/bash
# Copyright (c) 2010, bkil.hu
# created on 2010-12-30 20:04

# 930 kbit/s is adequate at a resolution of 400x300.
# 430 kbit/s gets blocky, but is still satisfactory if
#  played back with `mplayer -vf pp=ac`.

#vcodec=mpeg4:vbitrate=1200:mbd=1 \ 
#         -vf scale,pp=lb,denoise3d 
#mplayer -fs -fixed-vo -vo xv o-lavc*.avi

FREQ=$1
TRAPS="1 2 3"

PID=$$
ENC=""
#while true;do
 F="o-lavc-`date '+%Y%m%d-%H%M%S'`.avi"
 mencoder tv:// -endpos 12000 -quiet -o "$F" \
  -oac mp3lame -ovc lavc \
  -lavcopts vcodec=mpeg4:vbitrate=1304 \
  -lameopts cbr:br=96 \
  -vf pp=fd,crop=480:360,denoise3d \
  -tv width=512:height=384:alsa:adevice=hw.1,0:buffersize=150:freq=$FREQ & ENC=$!
 trap "kill $ENC; wait $ENC; trap - TERM $TRAPS; exit;" TERM
 for a in $TRAPS ;do
  trap "kill $ENC; wait $ENC; trap - TERM $TRAPS; kill -$a $PID;" $a
 done
 wait $ENC
#done
trap - TERM $TRAPS
